R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

We can load in our own data just as before, titling the code chunk (here I called it “data”) and if we don’t want the code chunk to be printed out, we can add echo = FALSE to the header.

##    Cross_ID          Cross_Type         Sample_ID          Dam_Weight_g  
##  Length:523         Length:523         Length:523         Min.   :13.68  
##  Class :character   Class :character   Class :character   1st Qu.:20.50  
##  Mode  :character   Mode  :character   Mode  :character   Median :22.83  
##                                                           Mean   :23.18  
##                                                           3rd Qu.:26.16  
##                                                           Max.   :30.21  
##                                                                          
##   Dam_Body_mm     Dam_Just_body    Dam_Tail_mm     Dam_Foot_mm   
##  Min.   : 82.65   Min.   :70.38   Min.   : 0.00   Min.   :11.60  
##  1st Qu.:137.37   1st Qu.:78.90   1st Qu.:58.32   1st Qu.:15.27  
##  Median :145.97   Median :82.28   Median :63.79   Median :16.11  
##  Mean   :142.31   Mean   :81.65   Mean   :60.96   Mean   :16.09  
##  3rd Qu.:154.08   3rd Qu.:84.53   3rd Qu.:68.65   3rd Qu.:16.98  
##  Max.   :166.53   Max.   :94.16   Max.   :78.22   Max.   :18.97  
##                                   NA's   :5                      
##    Dam_Ear_mm    Embryo_weight_g  Placenta_Weight_g Side_of_Repro     
##  Min.   : 7.81   Min.   :0.0723   Min.   :0.02120   Length:523        
##  1st Qu.: 9.62   1st Qu.:0.6038   1st Qu.:0.08260   Class :character  
##  Median :10.37   Median :0.7368   Median :0.09490   Mode  :character  
##  Mean   :10.39   Mean   :0.7808   Mean   :0.09994                     
##  3rd Qu.:11.04   3rd Qu.:0.9670   3rd Qu.:0.10750                     
##  Max.   :14.00   Max.   :2.1764   Max.   :0.36850                     
##                                                                       
##        TS             day            sex             N_in_litter   
##  Min.   :22.00   Min.   :14.00   Length:523         Min.   : 1.00  
##  1st Qu.:24.00   1st Qu.:16.00   Class :character   1st Qu.: 4.00  
##  Median :24.50   Median :16.50   Mode  :character   Median : 6.00  
##  Mean   :24.57   Mean   :16.56                      Mean   : 5.47  
##  3rd Qu.:25.00   3rd Qu.:17.00                      3rd Qu.: 7.00  
##  Max.   :26.50   Max.   :18.50                      Max.   :10.00  
##                                                                    
##   Experiment       
##  Length:523        
##  Class :character  
##  Mode  :character  
##                    
##                    
##                    
## 

GGplot2, or using similar syntax for a bunch of different plot types!

First, we are going to build the simplest of scatter plots

  ggplot(data = example_data, aes(x = Dam_Weight_g, y = Dam_Just_body)) + geom_point(alpha = .2)

Now let’s make our code easier to read!

example_data %>%
  ggplot(aes(x = Dam_Weight_g, y = Dam_Just_body)) + 
  geom_point()

We can get also add more features to the plot.

example_data %>%
  ggplot(aes(x = Dam_Weight_g, y = Dam_Just_body)) + 
  geom_point()+
  geom_smooth(method = lm)
## `geom_smooth()` using formula 'y ~ x'

example_data %>%
  ggplot(aes(x = Dam_Weight_g, y = Dam_Just_body, color = Experiment)) + 
  geom_point()+
  geom_smooth(method = lm)
## `geom_smooth()` using formula 'y ~ x'

We can get fancier than this. Let’s add some more categories to aesthetics

example_data %>%
  ggplot(aes(x = Dam_Weight_g, y = Dam_Just_body, color = Cross_Type, shape = Experiment)) + 
  geom_point()

Let’s try another plot type, with categorical data

library(beeswarm)

example_data %>%
  ggplot(aes(x = Cross_Type, y = Embryo_weight_g, color = Experiment)) + 
  geom_point()

example_data %>%
  ggplot(aes(x = Cross_Type, y = Embryo_weight_g, color = Experiment)) + 
  geom_violin()+
  geom_boxplot(width = 0.2, color = "black")+
  geom_quasirandom()

Let’s keep on making our plot fancy! You can change the background and gridlines quickly by using themes, update the angle of labels, switch the axis titles, etc.

example_data %>%
  ggplot(aes(x = Cross_Type, y = Embryo_weight_g, color = Experiment)) + 
  geom_violin()+
  geom_quasirandom()+
  geom_boxplot(width=0.2, color = "black", alpha = 0.01)+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5))+
  ylab("embryo weight (g)")+
  xlab("cross")

This is nice, but our variables aren’t ordered in our graph in a meaningful way. Let’s fix that!

library(forcats)

example_data %>%
  mutate(Cross_Type = fct_relevel(Cross_Type, 
            "TF.FM", "FM.TF",
            "CC.PP", "PP.CC", 
            "WW.LL", "LL.WW", 
            "PP.WW", "WW.PP", 
            "CC.WW", "WW.CC", 
            "PP.LL", "LL.PP", 
            "CC.LL", "LL.CC",
            "PP.TF", "TF.PP")) %>%
  ggplot(aes(x = Cross_Type, y = Embryo_weight_g, color = Experiment)) + 
  geom_violin()+
  geom_quasirandom()+
  geom_boxplot(width=0.2, color = "black")+
  theme_cowplot()+
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5))+
  ylab("embryo weight (g)")+
  xlab("cross")

Ugh, these colors tho. Lets try some other color options! R color brewer

example_data %>%
  mutate(Cross_Type = fct_relevel(Cross_Type, 
            "TF.FM", "FM.TF",
            "CC.PP", "PP.CC", 
            "WW.LL", "LL.WW", 
            "PP.WW", "WW.PP", 
            "CC.WW", "WW.CC", 
            "PP.LL", "LL.PP", 
            "CC.LL", "LL.CC",
            "PP.TF", "TF.PP")) %>%
  ggplot(aes(x = Cross_Type, y = Embryo_weight_g, color = Experiment)) + 
  geom_violin()+
  geom_quasirandom()+
  geom_boxplot(width=0.2, color = "black")+
  theme_cowplot()+
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5))+
  ylab("embryo weight (g)")+
  xlab("cross")+
  scale_color_brewer(palette = "Blues")

We can also set colors manually if you have hex codes or color names:

spret_color <-"#CACACA"
mus_color <- "#949494"
dom_color <-"#535360"
mus_dom_color <-"#74b3ce"
mus_spret_color <-"#94C973"

example_data %>%
  mutate(Cross_Type = fct_relevel(Cross_Type, 
            "TF.FM", "FM.TF",
            "CC.PP", "PP.CC", 
            "WW.LL", "LL.WW", 
            "PP.WW", "WW.PP", 
            "CC.WW", "WW.CC", 
            "PP.LL", "LL.PP", 
            "CC.LL", "LL.CC",
            "PP.TF", "TF.PP")) %>%
  ggplot(aes(x = Cross_Type, y = Placenta_Weight_g, color = Experiment)) + 
  geom_violin()+
  geom_quasirandom()+
  geom_boxplot(width=0.2, color = "black")+
  theme_cowplot()+
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5))+
  ylab("embryo weight (g)")+
  xlab("cross")+
  scale_color_manual(values = c(dom_color,
                                mus_color,
                                mus_dom_color,
                                mus_spret_color,
                                spret_color))

We can also use other packages with fun palettes, like Wes Anderson films or national parks

library(wesanderson)
library(NatParksPalettes)
## Registered S3 method overwritten by 'NatParksPalettes':
##   method        from       
##   print.palette wesanderson
######Wes Anderson####

pal <- wes_palette("FantasticFox1", n = 5)
example_data %>%
  mutate(Cross_Type = fct_relevel(Cross_Type, 
            "TF.FM", "FM.TF",
            "CC.PP", "PP.CC", 
            "WW.LL", "LL.WW", 
            "PP.WW", "WW.PP", 
            "CC.WW", "WW.CC", 
            "PP.LL", "LL.PP", 
            "CC.LL", "LL.CC",
            "PP.TF", "TF.PP")) %>%
  ggplot(aes(x = Cross_Type, y = Embryo_weight_g, color = Experiment)) + 
  geom_violin()+
  geom_quasirandom() +
  geom_boxplot(width=0.2, color = "black")+
  theme_cowplot()+
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5))+
  ylab("embryo weight (g)")+
  xlab("cross")+
  scale_color_manual(values = pal)

######National Parks####

pal <- natparks.pals("Yellowstone", n = 5)
example_data %>%
  mutate(Cross_Type = fct_relevel(Cross_Type, 
            "TF.FM", "FM.TF",
            "CC.PP", "PP.CC", 
            "WW.LL", "LL.WW", 
            "PP.WW", "WW.PP", 
            "CC.WW", "WW.CC", 
            "PP.LL", "LL.PP", 
            "CC.LL", "LL.CC",
            "PP.TF", "TF.PP")) %>%
  ggplot(aes(x = Cross_Type, y = Embryo_weight_g, color = Experiment)) + 
  geom_violin()+
  geom_quasirandom() +
  geom_boxplot(width=0.2, color = "black")+
  theme_cowplot()+
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5))+
  ylab("embryo weight (g)")+
  xlab("cross")+
  scale_color_manual(values = pal)

We can also color by continuous variables, either already plotted or in our data frame

######National Parks####
example_data %>%
  mutate(Cross_Type = fct_relevel(Cross_Type, 
            "TF.FM", "FM.TF",
            "CC.PP", "PP.CC", 
            "WW.LL", "LL.WW", 
            "PP.WW", "WW.PP", 
            "CC.WW", "WW.CC", 
            "PP.LL", "LL.PP", 
            "CC.LL", "LL.CC",
            "PP.TF", "TF.PP")) %>%
  ggplot(aes(x = Cross_Type, y = Embryo_weight_g, color = Placenta_Weight_g)) + 
  geom_violin()+
  geom_quasirandom() +
  geom_boxplot(width=0.2, color = "black")+
  theme_cowplot()+
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5))+
  ylab("embryo weight (g)")+
  xlab("cross")+
  scale_color_gradientn(colors=natparks.pals("Glacier"))

What if we want to highlight data groups in different panels? One way is to use facets

pal <- natparks.pals("Yellowstone", n = 5)
example_data %>%
  mutate(Cross_Type = fct_relevel(Cross_Type, 
            "TF.FM", "FM.TF",
            "CC.PP", "PP.CC", 
            "WW.LL", "LL.WW", 
            "PP.WW", "WW.PP", 
            "CC.WW", "WW.CC", 
            "PP.LL", "LL.PP", 
            "CC.LL", "LL.CC",
            "PP.TF", "TF.PP")) %>%
  ggplot(aes(x = Cross_Type, y = Embryo_weight_g, color = Experiment)) + 
  geom_violin()+
  geom_quasirandom() +
  geom_boxplot(width=0.2, color = "black")+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 45, vjust = 0.5))+
  ylab("embryo weight (g)")+
  xlab("cross")+
  scale_color_manual(values = pal)+
  facet_grid(Experiment ~ ., scales = "free")

We can also save out plots as objects, and then further make our panels nice!

pal <- natparks.pals("Yellowstone", n = 5)

Embryo_plot <- example_data %>%
                        mutate(Cross_Type = fct_relevel(Cross_Type, 
                                  "TF.FM", "FM.TF",
                                  "CC.PP", "PP.CC", 
                                  "WW.LL", "LL.WW", 
                                  "PP.WW", "WW.PP", 
                                  "CC.WW", "WW.CC", 
                                  "PP.LL", "LL.PP", 
                                  "CC.LL", "LL.CC",
                                  "PP.TF", "TF.PP")) %>%
                        ggplot(aes(x = Cross_Type, y = Embryo_weight_g, color = Experiment)) + 
                        geom_violin()+
                        geom_quasirandom() +
                        geom_boxplot(width=0.2, color = "black")+
                        theme_cowplot()+
                        theme(axis.text.x = element_text(angle = 45, vjust = 0.5), legend.position = "none")+
                        ylab("embryo weight (g)")+
                        xlab("cross")+
                        scale_color_manual(values = pal)
Placenta_plot <- example_data %>%
                        mutate(Cross_Type = fct_relevel(Cross_Type, 
                                  "TF.FM", "FM.TF",
                                  "CC.PP", "PP.CC", 
                                  "WW.LL", "LL.WW", 
                                  "PP.WW", "WW.PP", 
                                  "CC.WW", "WW.CC", 
                                  "PP.LL", "LL.PP", 
                                  "CC.LL", "LL.CC",
                                  "PP.TF", "TF.PP")) %>%
                        ggplot(aes(x = Cross_Type, y = Placenta_Weight_g, color = Experiment)) + 
                        geom_violin()+
                        geom_quasirandom() +
                        geom_boxplot(width=0.2, color = "black")+
                        theme_cowplot()+
                        theme(axis.text.x = element_text(angle = 45, vjust = 0.5))+
                        ylab("placenta weight (g)")+
                        xlab("cross")+
                        ylim(0,.4)+
                        scale_color_manual(values = pal)

plot_grid(Embryo_plot, Placenta_plot, labels = c("A","B"),rel_widths = c(1, 2))

GGplot2 can make so many more things!!!!

library(ggridges)

ridges <- example_data %>% 
   mutate(Cross_Type = fct_relevel(Cross_Type, 
                                  "TF.FM", "FM.TF",
                                  "CC.PP", "PP.CC", 
                                  "WW.LL", "LL.WW", 
                                  "PP.WW", "WW.PP", 
                                  "CC.WW", "WW.CC", 
                                  "PP.LL", "LL.PP", 
                                  "CC.LL", "LL.CC",
                                  "PP.TF", "TF.PP")) %>%
   ggplot(aes(x = Placenta_Weight_g, y = Cross_Type, fill = Experiment)) +
      geom_density_ridges(jittered_points = TRUE,
                          position = position_points_jitter(width = 0.05, height = 0),
                          point_shape = '|', 
                          point_size = 3, 
                          point_alpha = 1,
                          alpha = 0.5) +
      theme_ridges() + 
      theme(legend.position = "none")+
      #ggtitle("")+
      theme_bw()+
      xlab("placenta weight (g)")+
      ylab("cross")

ridges
## Picking joint bandwidth of 0.00595

We can also make plots interactive, to see the identity of specific points

library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
ggplotly(Placenta_plot)